package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.dto.StudentQueryDto;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import com.itheima.vo.CountVo;
import com.itheima.vo.SexVo;
import com.itheima.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    @Override
    public PageBean StudentQuery(StudentQueryDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<StudentVo> page = studentMapper.StudentQuery(dto);
        return new PageBean(page.getTotal(), page.getResult());

    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void insert(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount(0);
        student.setViolationScore(0);
        studentMapper.insert(student);
    }


    @Override
    public void update(Student student) {

        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);

    }

    @Override
    public void delete(List<Integer> ids) {
        /*删除多个不能直接用集合或者数组删除,
         * 要通过遍历一个一个删除*/
        ids.forEach(id -> studentMapper.delete(id)
        );
    }

    @Override
    public void violation(Integer id, Integer score) {

        studentMapper.violation(id, score);

    }

    @Override
    public List<SexVo> studentEducationData() {

        return studentMapper.studentEducationData();
    }

    @Override
    public CountVo studentCountData() {
        List<SexVo> list = studentMapper.countData();
        //使用stream流实现
        //遍历 List<ItemVo>， 将每个ItemVo的职位名称获取出来，形成一个新的List<String>
        List<String> names = list.stream().map(SexVo::getName).collect(Collectors.toList());
        //遍历 List<ItemVo>， 将每个ItemVo的职位数量获取出来，形成一个新的List<Integer>
        List<Integer> values = list.stream().map(SexVo::getValue).collect(Collectors.toList());

        return CountVo.builder().clazzList(names).dataList(values).build();
    }
}

